package org.jypj.dev.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;

import org.jypj.dev.dao.ICountTaskDao;
import org.jypj.dev.dao.IUserScoreDao;
import org.jypj.dev.dao.IUserTrainScoreDao;
import org.jypj.dev.dao.IUserTrainWorkScoreDao;
import org.jypj.dev.model.LoginInfo;
import org.jypj.dev.model.UserScore;
import org.jypj.dev.model.UserTrainScore;
import org.jypj.dev.model.UserTrainWorkScore;
import org.jypj.dev.model.UserWork;
import org.jypj.dev.service.ICountTaskService;
import org.jypj.dev.util.DateUtils;
import org.jypj.dev.util.StringUtil;
import org.jypj.dev.util.UserWorkUtils;
import org.springframework.stereotype.Service;

@Service("countTaskService")
public class CountTaskServiceImpl implements ICountTaskService {

    @Resource
    private ICountTaskDao countTaskDao;

    @Resource
    private IUserTrainWorkScoreDao userTrainWorkScoreDao;

    @Resource
    private IUserTrainScoreDao userTrainScoreDao;

    @Resource
    private IUserScoreDao userScoreDao;

    @Override
    public void countUserTrainScore() {
        Map<String, Object> queryMap = new HashMap<>();
        String todayStr = DateUtils.getTodayString2("yyyy-MM-dd");
        String tableName = StringUtil.getUserWorkTableNameFromDate(todayStr);
        queryMap.put("planDate", todayStr);
        queryMap.put("tableName", tableName);
        // 登录信息的集合
        List<LoginInfo> loginInfos = countTaskDao.selectAllLoginInfos(queryMap);
        Map<String, LoginInfo> loginInfoMap = getLoginInfoMaps(loginInfos);
        // 查询今天的所有数据
        List<UserWork> userWorks = countTaskDao.selectAllUserWorkToday(queryMap);

        Map<String, List<UserWork>> userWorkMaps = getUserWorkMaps(userWorks);
        // key的集合
        Set<String> keySet = userWorkMaps.keySet();
        // 这个也需要保存到数据库
        List<UserTrainScore> userTrainScores = new ArrayList<>();
        for (String key : keySet) {
            String loginId = key.substring(0, 32);
            String trainCode = key.substring(33);
            LoginInfo loginInfo = loginInfoMap.get(loginId);
            List<UserWork> uws = userWorkMaps.get(key);
            UserWorkUtils.initUserWorkScore(uws);
            // 这个需要保存到数据库
            List<UserTrainWorkScore> userTrainWorkScores = initUserWorkDepth1Score(uws, loginInfo);
            userTrainWorkScoreDao.saveUserTrainWorkScores(userTrainWorkScores);
            int sysScore = getScoreFromUserWorks(uws);
            UserTrainScore uts = new UserTrainScore();
            String id = UUID.randomUUID().toString().replace("-", "");
            uts.setId(id);
            uts.setLoginId(loginId);
            uts.setPlanTime(loginInfo.getLoginTime().substring(0, 10));
            uts.setSysScore(sysScore);
            uts.setFinalScore(sysScore);
            uts.setTrainCode(trainCode);
            uts.setTrainNum(loginInfo.getTrainNum());
            uts.setUserNum(loginInfo.getUserNum());
            uts.setUserName(loginInfo.getUserName());
            userTrainScores.add(uts);
        }
        if (userTrainScores.size() > 0) {
            userTrainScoreDao.saveUserTrainScores(userTrainScores);
        }

    }

    /**
     * 保存用户分数
     */
    @Override
    public void countUserScore() {
        Map<String, Object> queryMap = new HashMap<>();
        String yesTodayStr = DateUtils.getYestodayString2("yyyy-MM-dd");
        String tableName = StringUtil.getUserWorkTableNameFromDate(yesTodayStr);
        queryMap.put("planDate", yesTodayStr);
        queryMap.put("tableName", tableName);
        // 最后是用户当天得分
        List<UserScore> userScores = countTaskDao.selectAllUserScore(queryMap);
        for (UserScore userScore : userScores) {
            String id = UUID.randomUUID().toString().replace("-", "");
            userScore.setId(id);
        }
        if (userScores.size() > 0) {
            userScoreDao.saveUserScores(userScores);
        }

    }

    /**
     * 将loginInfos转为相应的map
     *
     * @param loginInfos
     * @return
     */
    public Map<String, LoginInfo> getLoginInfoMaps(List<LoginInfo> loginInfos) {
        Map<String, LoginInfo> map = new HashMap<>();

        for (LoginInfo loginInfo : loginInfos) {
            map.put(loginInfo.getId(), loginInfo);
        }
        return map;
    }

    /**
     * 从userWorks 得到集合
     *
     * @param userWorks
     * @return
     */
    public Map<String, List<UserWork>> getUserWorkMaps(List<UserWork> userWorks) {

        // key存放loginId-trainCode
        // value存放所有用户工序的集合
        Map<String, List<UserWork>> userWorkMaps = new HashMap<>();

        // 首先将查询出来的用户工序集合存放在map集合中
        for (UserWork userWork : userWorks) {
            String key = userWork.getLoginId() + "-" + userWork.getTrainCode();
            List<UserWork> list = userWorkMaps.get(key);
            if (null == list) {
                list = new ArrayList<>();
                list.add(userWork);
                userWorkMaps.put(key, list);
            } else {
                userWorkMaps.get(key).add(userWork);
            }
        }

        return userWorkMaps;
    }

    /**
     * 设置userWork中一级工序的分数
     *
     * @param userWorks
     */
    public List<UserTrainWorkScore> initUserWorkDepth1Score(List<UserWork> userWorks, LoginInfo loginInfo) {
        List<UserTrainWorkScore> userTrainWorkScores = new ArrayList<>();
        for (UserWork userWork : userWorks) {
            if (userWork.getCatalogDepth() == 1) {
                UserTrainWorkScore userTrainWorkScore = new UserTrainWorkScore();
                int score = userWork.getScore();
                userWork.setScore(score);
                String id = UUID.randomUUID().toString().replace("-", "");
                userTrainWorkScore.setId(id);
                userTrainWorkScore.setLoginId(loginInfo.getId());
                userTrainWorkScore.setPlanTime(loginInfo.getLoginTime().substring(0, 10));
                userTrainWorkScore.setSysScore(100 - score);
                userTrainWorkScore.setTrainCode(userWork.getTrainCode());
                userTrainWorkScore.setTrainNum(loginInfo.getTrainNum());
                userTrainWorkScore.setTrainWorkId(userWork.getTrainWorkId());
                userTrainWorkScore.setWorkId(userWork.getWorkId());
                userTrainWorkScore.setWorkName(userWork.getWorkName());
                userTrainWorkScore.setUserName(loginInfo.getUserName());
                userTrainWorkScore.setUserNum(loginInfo.getUserNum());
                userTrainWorkScores.add(userTrainWorkScore);
            }
        }
        return userTrainWorkScores;
    }

    /**
     * 根据得分计算用户的得分情况
     *
     * @param userWorks
     * @return
     */
    public int getScoreFromUserWorks(List<UserWork> userWorks) {
        int average = 0;
        if (userWorks.size() > 0) {
            int sum = 0;
            int num = 0;
            for (UserWork userWork : userWorks) {
                if (userWork.getCatalogDepth() == 1) {
                    num++;
                    sum = sum + (100 - userWork.getScore());
                }
            }
            average = sum / num;
        }
        return average;
    }

}
